﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace FigureDraw
{
    public class GDIPlusAdaptee
    {


        public GDIPlusAdaptee()
        {
            // TODO: Complete member initialization
        }

        private Form drawForm;
        public void DrawLine(Paper drawPaper, Vector2 oldP, Vector2 curP, PenType penType)
        {
            drawForm = new Form();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(drawForm))
            {
                drawForm = (Form)drawPaper.getDrawPaper();
            }
            Graphics myGraphics = drawForm.CreateGraphics();
            Pen myPen = new Pen(Color.Black);
            Point p1 = new Point(oldP.X, oldP.Y);
            Point p2 = new Point(curP.X, curP.Y);
            myGraphics.DrawLine(myPen, p1, p2);

            myPen.Dispose();
            myGraphics.Dispose();
        }

        internal void DrawEclipse(Paper drawPaper, Vector2 oldP, Vector2 curP, PenType penType)
        {
            drawForm = new Form();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(drawForm))
            {
                drawForm = (Form)drawPaper.getDrawPaper();
            }
            Graphics myGraphics = drawForm.CreateGraphics();
            Pen myPen = new Pen(Color.Black);
            Point p1 = new Point(oldP.X, oldP.Y);
            Point p2 = new Point(curP.X, curP.Y);
            Rectangle rect = new Rectangle(p1.X,p1.Y,Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
            myGraphics.DrawEllipse(myPen, rect);
        }

        
        internal void DrawRectangle(Paper drawPaper, Vector2 oldP, Vector2 curP, PenType penType)
        {
            drawForm = new Form();
            if (drawPaper.getDrawPaper().GetType().IsInstanceOfType(drawForm))
            {
                drawForm = (Form)drawPaper.getDrawPaper();
            }
            Graphics myGraphics = drawForm.CreateGraphics();
            Pen myPen = new Pen(Color.Black);
            Point p1 = new Point(oldP.X, oldP.Y);
            Point p2 = new Point(curP.X, curP.Y);

            myGraphics.DrawRectangle(myPen, p1.X, p1.Y, Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
        }

        internal void SavePNG(string filePath)
        {
            Bitmap bmpPicture = new Bitmap(1000, 1000);
            Graphics g = Graphics.FromImage(bmpPicture);
            Rectangle rect = drawForm.RectangleToScreen(drawForm.ClientRectangle);

            g.CopyFromScreen(rect.Location, Point.Empty, drawForm.Size);

            bmpPicture.Save(filePath);
        }
    }
}
